# -*- coding: utf-8 -*-

from PyQt4.QtGui import QDialog
from PyQt4.QtCore import SIGNAL
from PyQt4.Qt import QRect, QDesktopWidget
from PyQt4.QtCore import QCoreApplication, QEventLoop
from ui_projectcreate import Ui_ProjectCreate
from subprocess import *
from mainfunctions import *
from githg import *
from cvssvn import *

connection = sqlite.connect('commits.db')
cursor = connection.cursor()

selectedVCS = ""
projectRootUrl = ""

projectName = ""
projectVersion = 0

class ProjectCreate(QDialog):
    def __init__(self, parent=None):
        QDialog.__init__(self, parent)
        self.ui = Ui_ProjectCreate()
        self.ui.setupUi(self)
        
        available_geom = QDesktopWidget().availableGeometry()
        current_geom = self.frameGeometry()
        self.setGeometry(available_geom.width() / 2 - current_geom.width() / 2, available_geom.height() / 2 - current_geom.height() / 2, current_geom.width(), current_geom.height());
        
        self.connect(self.ui.projCreateOkBtn, SIGNAL("clicked()"), self.createProject)
        self.connect(self.ui.projCreateCancelBtn, SIGNAL("clicked()"), self.cancelButton)

    def setSelectedVCS(self, currentVCS):
        global selectedVCS
        selectedVCS = currentVCS

    def setProjectRootUrl(self, currentPath):
        global projectRootUrl
        projectRootUrl = currentPath
        
    def cancelButton(self):
        self.hide()
        
    def listProjects1(self):
        listProjects1(self.browser)

    def createProject(self):
        global selectedVCS
        global projectRootUrl
        rootDirectory = "fooo"
        
        if (len(self.ui.projName.text()) != 0) or (len(self.ui.projVersion.text()) != 0):
            projectName = str(self.ui.projName.text())
            projectVersion = str(self.ui.projVersion.text())
            isSuccessful = createProject(self.ui.projName.text(), self.ui.projVersion.text(), self.browser)
            
            if(isSuccessful == 5555):
                self.hide()
                self.ui.projName.clear()
                self.ui.projVersion.clear()
                isSuccessful = 0

                if(selectedVCS == "CVS"):
                    self.browser.append("CVS Log is going to be parsed now...\n\nData will be stored with:\n\nProject Name: " + projectName + "\tProject Version: " + projectVersion)
                    QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)
                    parseCvsLog(projectName, projectVersion, projectRootUrl, rootDirectory, self.browser)
                    self.browser.append("\n\nCVS Log is parsed and stored with:\n\nProject Name: " + projectName + "\tProject Version: " + projectVersion)
                    QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)
            
                elif(selectedVCS == "Git"):                    
                    self.browser.append("Git Log is going to be parsed now...\n\nData will be stored with:\n\nProject Name: " + projectName + "\tProject Version: " + projectVersion)
                    QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)
                    parseGitLog(projectName, projectVersion, projectRootUrl, self.browser)
                    self.browser.append("\n\nGit Log is parsed and stored with:\n\nProject Name: " + projectName + "\tProject Version: " + projectVersion)
                    QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)

                elif(selectedVCS == "Mercurial"):
                    self.browser.append("Mercurial Log is going to be parsed now...\n\nData will be stored with:\n\nProject Name: " + projectName + "\tProject Version: " + projectVersion)
                    QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)
                    parseMercurialLog(projectName, projectVersion, projectRootUrl, self.browser)
                    self.browser.append("\n\nMercurial Log is parsed and stored with:\n\nProject Name: " + projectName + "\tProject Version: " + projectVersion)
                    QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)
                    
                elif(selectedVCS == "Subversion"):
                    self.browser.append("Subversion Log is going to be parsed now...\n\nData will be stored with:\n\nProject Name: " + projectName + "\tProject Version: " + projectVersion)
                    QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)
                    parseSubversionLog(projectName, projectVersion, projectRootUrl, rootDirectory, self.browser)
                    self.browser.append("\n\nSubversion Log is parsed and stored with:\n\nProject Name: " + projectName + "\tProject Version: " + projectVersion)
                    QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)
                    
                
               
